<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="id?'编辑月卡':'添加月卡'" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="carinfoform" label-width="100px" :model="carinfoform" :rules="rules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="carinfoform.personName" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="carinfoform.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="carinfoform.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="carinfoform.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="cartimeinfo" label-width="100px" :model="cartimeinfo" :rules="rules2">
            <el-form-item label="有效日期" prop="timedata">
              <el-date-picker
                v-model="cartimeinfo.timedata"
                unlink-panels
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="cartimeinfo.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="cartimeinfo.paymentMethod">
                <el-option
                  v-for="item in patoptions"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetform">重置</el-button>
        <el-button type="primary" @click="addcar">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addcarcardAPI, cardetailAPI, editcarcardAPI } from '@/api/xingcard'
export default {
  name: 'AddCard',
  data() {
    return {
      carinfoform: {
        personName: '',
        phoneNumber: '17610002222',
        carNumber: '京B55555',
        carBrand: ''
      },
      rules: {
        personName: [
          { required: true, message: '请输入车主姓名', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '请输入车辆号码', trigger: 'blur' },
          { validator: this.validatenumber, trigger: 'blur' }
        ],
        carBrand: [
          { required: true, message: '请输入车辆品牌', trigger: 'blur' }
        ]
      },
      cartimeinfo: {
        timedata: [],
        paymentAmount: null,
        paymentMethod: ''
      },
      rules2: {
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' },
          { pattern: /^(?:0|(?:-?[1-9]\d*))$/, message: '请输入数字', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'change' }
        ],
        timedata: [
          { required: true, message: '请填写日期' }
        ]
      },
      patoptions: [
        { value: 'Alipay', label: '支付宝' },
        { value: 'WeChat', label: '微信' },
        { value: 'Cash', label: '线下' }
      ]
    }
  },
  // 获取路由ID
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  created() {
    if (this.id) {
      this.cardetaiedit()
    }
  },
  methods: {
    // 编辑月卡
    async cardetaiedit() {
      const res = await cardetailAPI(this.id)
      const { data } = res
      const { personName, phoneNumber, carNumber, carBrand, carInfoId } = data
      const { cardStartDate, cardEndDate, paymentAmount, paymentMethod, rechargeId } = data
      this.carinfoform = { personName, phoneNumber, carNumber, carBrand, carInfoId }
      this.cartimeinfo = {
        timedata: [cardStartDate, cardEndDate],
        paymentAmount,
        paymentMethod,
        rechargeId
      }
      // console.log(this.carinfoform)
      // console.log(this.cartimeinfo)
    },
    // 自定义表单校验
    validatenumber(rule, value, callback) {
      const plateNumberRegex = /^[\u4E00-\u9FA5][\da-zA-Z]{6}$/
      if (plateNumberRegex.test(value)) {
        callback()
      } else {
        callback(new Error('检查车辆号码'))
      }
    },
    // 重置表单
    resetform() {
      this.$refs.carinfoform.resetFields()
      this.$refs.cartimeinfo.resetFields()
      // console.log(132)
    },
    // 表单兜底校验
    addcar() {
      this.$refs.carinfoform.validate((flag) => {
        if (!flag) return
        this.$refs.cartimeinfo.validate(async(flag1) => {
          if (!flag1) return
          // console.log('发请求')
          const cardStartDate = this.cartimeinfo.timedata[0]
          const cardEndDate = this.cartimeinfo.timedata[1]
          const addobj = {
            ...this.carinfoform,
            ...this.cartimeinfo,
            cardStartDate,
            cardEndDate
          }
          delete addobj.timedata
          // console.log(addobj)
          if (this.id) {
            await editcarcardAPI(addobj)
            this.$message.success('编辑成功')
            // console.log(res)
          } else {
            await addcarcardAPI(addobj)
            this.$message.success('添加成功')
          }
          this.$router.go(-1)
          this.resetform()
          // console.log(res)
        })
      })
    }
  }

}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
